package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysContractRefund;
import com.ruoyi.system.service.ISysContractRefundService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysContractRefundReportMapper;
import com.ruoyi.system.domain.SysContractRefundReport;
import com.ruoyi.system.service.ISysContractRefundReportService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

/**
 * 挽单报告Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-01
 */
@Service
public class SysContractRefundReportServiceImpl implements ISysContractRefundReportService
{
    @Autowired
    private SysContractRefundReportMapper sysContractRefundReportMapper;

    /** 退费合同service接口层 */
    @Autowired
    private ISysContractRefundService sysContractRefundService;

    /**
     * 查询挽单报告
     *
     * @param id 挽单报告主键
     * @return 挽单报告
     */
    @Override
    public SysContractRefundReport selectSysContractRefundReportById(Long id)
    {
        return sysContractRefundReportMapper.selectSysContractRefundReportById(id);
    }

    /**
     * 根据退费合同主键查询挽单
     * @param refundId
     * @return
     */
    @Override
    public SysContractRefundReport selectSysContractRefundReportByContractRefundId(Long refundId) {
        return sysContractRefundReportMapper.selectSysContractRefundReportByContractRefundId(refundId);
    }

    /**
     * 查询挽单报告列表
     *
     * @param sysContractRefundReport 挽单报告
     * @return 挽单报告
     */
    @Override
    public List<SysContractRefundReport> selectSysContractRefundReportList(SysContractRefundReport sysContractRefundReport)
    {
        return sysContractRefundReportMapper.selectSysContractRefundReportList(sysContractRefundReport);
    }

    /**
     * 新增挽单报告
     *
     * @param sysContractRefundReport 挽单报告
     * @return 结果
     */
    @Override
    @Transactional
    public int insertSysContractRefundReport(SysContractRefundReport sysContractRefundReport)
    {
        int status = 0;
        try {
            //判断方法
            emptyInsertSysContractRefundReport(sysContractRefundReport);

            //查询退费合同
            SysContractRefund sysContractRefund = sysContractRefundService.selectSysContractRefundById(sysContractRefundReport.getContractRefundId());
            if (null == sysContractRefund){
                throw new RuntimeException("退费合同未查询到，请联系管理员");
            }

            //判断状态是否为（0：新建）、（1：待校长审批）
            if (sysContractRefund.getStatus() != 0 && sysContractRefund.getStatus() != 1){
                throw new RuntimeException("当前退费合同已提交不可修改");
            }

            //判断是新增还是修改方法
            if (null == sysContractRefundReport.getId()){
                //退费合同状态改成1:待校长审批
                status = this.insertSysContractRefundReport(sysContractRefundReport);
            }
            //修改方法
            else {
                status = this.updateSysContractRefundReport(sysContractRefundReport);
            }

            //退费合同状态改成1:待校长审批
            sysContractRefund.setStatus(1L);
            //退费合同修改方法
            sysContractRefundService.updateSysContractRefund(sysContractRefund);
        }
        catch (Exception e){
            //事务回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            throw new RuntimeException(e.getMessage());
        }
        return status;
    }

    /**
     * 新增挽单报告判断方法
     * @param sysContractRefundReport
     */
    @Override
    public void emptyInsertSysContractRefundReport(SysContractRefundReport sysContractRefundReport) {
        //合同主键
        if (null == sysContractRefundReport){
            throw new RuntimeException("合同主键未传输");
        }
        //文本
        if (StringUtils.isEmpty(sysContractRefundReport.getContext())){
            throw new RuntimeException("退费过程及原因分析未填写");
        }
    }

    /**
     * 修改挽单报告
     *
     * @param sysContractRefundReport 挽单报告
     * @return 结果
     */
    @Override
    public int updateSysContractRefundReport(SysContractRefundReport sysContractRefundReport)
    {
        return sysContractRefundReportMapper.updateSysContractRefundReport(sysContractRefundReport);
    }

    /**
     * 批量删除挽单报告
     *
     * @param ids 需要删除的挽单报告主键
     * @return 结果
     */
    @Override
    public int deleteSysContractRefundReportByIds(Long[] ids)
    {
        return sysContractRefundReportMapper.deleteSysContractRefundReportByIds(ids);
    }

    /**
     * 删除挽单报告信息
     *
     * @param id 挽单报告主键
     * @return 结果
     */
    @Override
    public int deleteSysContractRefundReportById(Long id)
    {
        return sysContractRefundReportMapper.deleteSysContractRefundReportById(id);
    }

    /**
     * 根据退费单合同主键删除挽单
     * @param refundId
     * @return
     */
    @Override
    public int deleteSysContractRefundReportByRefundId(Long refundId) {
        return sysContractRefundReportMapper.deleteSysContractRefundReportByRefundId(refundId);
    }
}